<?php
/**
 * Created by PhpStorm.
 * User: Mak
 * Date: 2016/3/12
 * Time: 13:47
 */

namespace Home\Controller;

use Think\Controller;
use Think\Verify;

class LoginController extends Controller
{
    public function index()
    {

        $this->display();
    }

    public function ver()
    {
        $Verify = new Verify();
        $Verify->reset = true;
        $Verify->length = 4;
        $Verify->entry();
    }

    public function login()
    {
        $username = htmlspecialchars(trim(I("post.username")));
        $password = htmlspecialchars(trim(I("post.password")));
        $yzm = htmlspecialchars(trim(I("post.yzm")));
        if ($this->check_verify($yzm)) {
            $stuInfo = D("StuInfo");
            $res = $stuInfo->checkUser($username, $password);
            if ($res == 1) {
                session("stuid", $username);
                redirect(U('Index/index'));
            } elseif ($res == 0) {
                $this->error("密码错误！");
            } elseif ($res == -1) {
                $this->error("帐号不存在！");
            }
        } else {
            $this->error("验证码出错！");
        }

    }

    public function logout()
    {
        session(null);
        redirect(U('Index/index'));
    }

    public function putClass()
    {

    }

    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
}